﻿using System.Collections.Generic;
using System.Linq;

using Infragistics.Documents.Excel;

using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.ExcelReader.ExcelReaderFactory;
using VNet.VisiSuite.ExcelReader.Models;

namespace VNet.VisiSuite.ExcelReader.ExcelReader
{
    /// <summary>
    /// Product Reader
    /// </summary>
    public class ProductReader : IExcelReaderFactory
    {
        /// <summary>
        /// The _product name index
        /// </summary>
        private int _productNameIndex = -1;
        /// <summary>
        /// The _enable index
        /// </summary>
        private int _enableIndex = -1;
        /// <summary>
        /// The _setup index
        /// </summary>
        private int _setupIndex = -1;
        /// <summary>
        /// The _work content index
        /// </summary>
        private int _workContentIndex = -1;

        /// <summary>
        /// Reads the excel sheet.
        /// </summary>
        /// <param name="workbook">The workbook.</param>
        /// <returns></returns>
        public IExcelData ReadExcelSheet(Workbook workbook)
        {
            var worksheet = workbook.Worksheets[0];
            if (worksheet == null)
            {
                FileLog.Fatal("Error: First sheet cannot be read.");
                return null;
            }

            var rowCollection = worksheet.Rows;
            var model = new ProductData
                {
                    Items = new List<ProductModelItem>(),
                };
            if (!rowCollection.Any())
            {
                return model;
            }
            var headerRow = rowCollection[0];
            GetColumnIndexes(headerRow.Cells);

            for (var i = 1; i < rowCollection.Count(); i++)
            {
                var row = worksheet.Rows[i];
                var workContentNumber = 0;
                if (_workContentIndex >= 0)
                {
                    var workContent = row.Cells[_workContentIndex].Value == null
                                      ? "0"
                                      : row.Cells[_workContentIndex].GetText();
                    int.TryParse(workContent, out workContentNumber);
                }
                var enableValue = true;
                if (_enableIndex >= 0)
                {
                    var enable = row.Cells[_enableIndex].Value == null
                                 ? "false"
                                 : row.Cells[_enableIndex].GetText().Trim();
                    bool.TryParse(enable, out enableValue);
                }
                
                var setUpValue = 0;
                if (_setupIndex >= 0)
                {
                    var setup = row.Cells[_setupIndex].Value == null
                                    ? "0"
                                    : row.Cells[_setupIndex].GetText().Trim();
                    int.TryParse(setup, out setUpValue);
                }

                var item = new ProductModelItem
                {
                    PartTime = workContentNumber,
                    Sku = (_productNameIndex < 0 || row.Cells[_productNameIndex].Value == null) ? null : row.Cells[_productNameIndex].GetText(),
                    Enabled = enableValue,
                    SetupTime = setUpValue,
                    CycleTime = 0,
                    LoadUnloadTime = 0
                };
                model.Items.Add(item);
            }
            return model;
        }

        /// <summary>
        /// Gets the column indexes.
        /// </summary>
        /// <param name="cells">The cells.</param>
        private void GetColumnIndexes(WorksheetCellCollection cells)
        {
            for (var i = 0; i < cells.Count(); i++)
            {
                var text = cells[i].GetText();
                if (!string.IsNullOrEmpty(text))
                {
                    switch (text.ToUpper().Trim())
                    {
                        case "PRODUCT NAME":
                        case "PRODUCTNAME":
                        case "SKU":
                            _productNameIndex = i;
                            break;
                        case "ENABLED":
                        case "ENABLE":
                            _enableIndex = i;
                            break;
                        case "SETUP":
                        case "SET UP":
                            _setupIndex = i;
                            break;
                        case "WORK CONTENT":
                        case "WORKCONTENT":
                            _workContentIndex = i;
                            break;
                    }
                }
            }
        }
    }
}
